package com.meilai.project.core.excel;

import cn.hutool.core.util.StrUtil;
import com.meilai.project.core.excel.config.QueryParamConfig;
import com.meilai.project.core.excel.config.TableHeaderConfig;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xchen
 * @since 2022-02-15
 **/
@SuppressWarnings("rawtypes")
@Getter
@Slf4j
public class ReportSheet {

	//sheet标签名
	private String sheetName = "方案列表导出";

	//报表标题
	private String reportTitle;

	//查询参数显示配置
	private final List<QueryParamConfig> queryParamConfigList = new ArrayList<>();

	//表头配置
	private final List<TableHeaderConfig> tableHeaderConfigList = new ArrayList<>();

	public static ReportSheet create(String reportTitle) {
		ReportSheet reportSheet = new ReportSheet();
		reportSheet.reportTitle = reportTitle;
		return reportSheet;
	}

	public static ReportSheet create() {
		return create(null);
	}

	public ReportSheet reportTitle(String reportTitle) {
		this.reportTitle = reportTitle;
		return this;
	}

	public ReportSheet addQueryParamConfig(List<QueryParamConfig> paramConfigList) {
		this.queryParamConfigList.addAll(paramConfigList);
		return this;
	}

	public ReportSheet addQueryParamConfig(QueryParamConfig paramConfig) {
		this.queryParamConfigList.add(paramConfig);
		return this;
	}

	public ReportSheet addTableHeaderConfig(List<TableHeaderConfig> tableHeaderConfigList) {
		this.tableHeaderConfigList.addAll(tableHeaderConfigList);
		return this;
	}

	public ReportSheet addTableHeaderConfig(TableHeaderConfig tableHeaderConfig) {
		this.tableHeaderConfigList.add(tableHeaderConfig);
		return this;
	}

	public void init() {
		assertConfigValid();
		checkOrInitName();
	}

	private void checkOrInitName() {
		if (StrUtil.isBlank(getSheetName())) {
			this.sheetName = getReportTitle();
		}
	}

	private void assertConfigValid() {
		if (null == getTableHeaderConfigList() || getTableHeaderConfigList().size() == 0) {
			log.error("未设置导出报表的列头，请设置");
			throw new ExportException("导出失败, 原因:没有设置列头");
		}
	}


}
